package com.leo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
 * @ClassName ListViewDemo
 * @Description
 * @Author wangss
 * @date 2020.06.09 11:04
 * @Version 1.0
 */
public class ListViewDemo4 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        ObservableList<Student> list = FXCollections.observableArrayList();

        for (int i = 0; i < 10; i++) {
            Student student = new Student("wangss - " + i);
            list.add(student);
        }

        ListView<Student> listView = new ListView<>();
        listView.setPlaceholder(new Label("没有数据"));
        listView.setPrefWidth(300d);
        listView.setPrefWidth(300d);

        listView.setItems(list);

        Button button = new Button("button");

        an.getChildren().addAll(button, listView);

        AnchorPane.setTopAnchor(listView, 50d);
        AnchorPane.setLeftAnchor(listView, 100d);

        primaryStage.setScene(scene);
        primaryStage.setTitle("ListView");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.setResizable(false);
        primaryStage.show();

        listView.setEditable(true);

        Callback<ListView<Student>, ListCell<Student>> listViewListCellCallback = ComboBoxListCell.forListView(new StringConverter<Student>() {
            @Override
            public String toString(Student object) {
                return object.getName();
            }

            @Override
            public Student fromString(String string) {
                return new Student(string);
            }
        }, new Student("ztt"), new Student("xyy"));

        listView.setCellFactory(listViewListCellCallback);
    }
}
